package corpus;

import model.VariableStatiques;
import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Reorder;
import weka.filters.unsupervised.attribute.StringToWordVector;

public class StringToVector {
	
	private StringToWordVector s2wv;
	
	public StringToVector(){
		this(1000, false);
	}

	public StringToVector(int nbWordsToKeep, boolean idfTransform) {
		s2wv = new StringToWordVector(nbWordsToKeep);
		s2wv.setIDFTransform(idfTransform);
		s2wv.setTFTransform(idfTransform);
		s2wv.setDoNotOperateOnPerClassBasis(false);
		s2wv.setInvertSelection(false);
		s2wv.setLowerCaseTokens(true);
		s2wv.setMinTermFreq(1);
		s2wv.setUseStoplist(false);
		s2wv.setDoNotOperateOnPerClassBasis(true);
	}
	
	public StringToVector(int nbWordsToKeep){
		s2wv = new StringToWordVector(nbWordsToKeep);
		s2wv.setIDFTransform(false);
		s2wv.setTFTransform(false);
		s2wv.setDoNotOperateOnPerClassBasis(false);
		s2wv.setInvertSelection(false);
		s2wv.setLowerCaseTokens(true);
		s2wv.setMinTermFreq(1);
		s2wv.setUseStoplist(false);
	}
	
	
	public Instances outputFilter(Instances corpus) throws Exception{
		
		Instances inst = Filter.useFilter(corpus, s2wv);
		Reorder re = new Reorder();
		String order = "";
		
		for(int i = 2; i < inst.numAttributes()+1; i++){
			if(!order.equals("")){
				order += ",";
			}
			
			order+= Integer.toString(i);
		}
		
		if(!order.equals("")){
			order += ",";
		}
		
		order += Integer.toString(1);
		System.out.println(order);
		re.setAttributeIndices(order);
		re.setInputFormat(inst);
		inst = Filter.useFilter(inst, re);;
		return inst;
	}
	
	
	public void setInputFormat(){
		try{
			s2wv.setInputFormat(VariableStatiques.FILTER_INSTANCES);
		}
		catch (Exception e){
			e.printStackTrace();
		}
	}
	
	public Instances outputSearchFilter(Instances corpus) throws Exception{
		Instances inst = Filter.useFilter(corpus, s2wv);
		return inst;
	}
	
	public void setWordsToKeep(int x){
		s2wv.setWordsToKeep(x);
	}
	
	public int getWordsToKeep(){
		return s2wv.getWordsToKeep();
	}
		
}
	
	

